package com.hh;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.hehe.AuthServerApplication;
import com.hehe.powernode.util.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.nio.file.Files;

//@SpringBootTest
/**
 * 实现阿里云oss文件上传
 */
@SpringBootTest(classes = AuthServerApplication.class)
@Slf4j
public class OSSUploader {
//              .accessKeyId("LTAI5tMsaW47cyX2rCnZ9SdV")
//                .accessKeySecret("QpBcvRbYFJbFlJEwBpYR7AgxLhBqi1")
    private static final String ENDPOINT = "oss-cn-beijing.aliyuncs.com"; // 替换为你的 endpoint
    private static final String ACCESS_KEY_ID = "LTAI5tMsaW47cyX2rCnZ9SdV"; // 替换为你的 Access Key ID
    private static final String ACCESS_KEY_SECRET = "QpBcvRbYFJbFlJEwBpYR7AgxLhBqi1"; // 替换为你的 Access Key Secret
    private static final String BUCKET_NAME = "bucketldd"; // 替换为你的 Bucket 名称

    private OSS ossClient;

    public OSSUploader() {
        ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
    }

    public void uploadFile(String filePath, String objectName) {
        // 上传文件
        ossClient.putObject(BUCKET_NAME, objectName, new File(filePath));
        System.out.println("File uploaded successfully.");
    }

    public void shutdown() {
        // 关闭 OSS 客户端
        ossClient.shutdown();
    }
    public static void main(String[] args) {
        OSSUploader uploader = new OSSUploader();
        String filePath = "D:\\java-project\\spring-emq\\test.txt"; // 本地文件路径
        String objectName = "folder/file.txt"; // 在 OSS 中的文件名

        uploader.uploadFile(filePath, objectName);
        uploader.shutdown();
    }

    @Autowired
    private OssUtil ossUtil;
    /**
     * 上传文件到阿里云 OSS
     * @return 上传后的文件访问 URL
     */
    @Test
    public void testUpload() throws IOException {
        log.info("ossUtil--------ossUtil:{}",ossUtil);
        // 获取文件输入流和文件名
        String filePath = "D:\\java-project\\spring-emq\\test.txt"; // 本地文件路径
        File file = new File(filePath);
        InputStream inputStream =new FileInputStream(filePath);

        String string = ossUtil.uploadFile(inputStream, filePath);
        log.info("------testUpload--string:{}",string);

    }

}
